package ru.classbase.dbdumper.storage

import org.springframework.stereotype.Component
import ru.classbase.dbdumper.AppProperties
import ru.classbase.dbdumper.getFileExtension
import ru.classbase.dbdumper.model.DbMetaData
import ru.classbase.dbdumper.model.StorageContext
import ru.classbase.dbdumper.model.StorageType
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption

@Component
class DirectoryStorage(
    private val props: AppProperties
) : DumpStorage {

    override val supportedStorage = StorageType.DIRECTORY

    override fun uploadFrom(meta: DbMetaData, ctx: StorageContext) {
        val outputDir = Files.createDirectories(Path.of(props.directory.outputDir, meta.database, ctx.schema))

        val ext = getFileExtension(props.outputFormat, props.compressionType)
        val outFile = outputDir.resolve("${ctx.tableName}.$ext")

        Files.copy(ctx.input, outFile, StandardCopyOption.REPLACE_EXISTING)
    }
}